package org.myorg.v6;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Calendar;

import org.myorg.v4.F42;
import org.myorg.v4.F43;
import org.myorg.v4.FEvolution4;
import org.myorg.v4.FPeople4;
import org.myorg.v4.Fc4;
import org.myorg.v4.Para4;


public class GA6 {
	static int best_p;
	static double best_v;
	static FPeople4 best_people;


	public static String ga(int geneLength,
			int peopleNum, double p_cross,
			double p_mut, long i_num,Fc4 fc) {

		String res = "";
		String value = "";
		String time = "";
		String it = "";
		double g_b = Double.NEGATIVE_INFINITY;
		double[] data = { 0.99, 0.999, 0.9999, 0.99999};
		//double[] data = { 0.99, 0.999, 0.9999, 0.99999, 0.999999, 0.9999999, 0.99999999};
		int count=0;

		long start_time = System.currentTimeMillis();
		FPeople4 my_p = new FPeople4(peopleNum,geneLength);
		FEvolution4 my_e = new FEvolution4(0.1, p_cross, p_mut);

		fc.caculate(my_p.persons);
		for (int i=0; i < i_num && count < data.length; i++) {

			if (my_p.getBest().fitness >= data[count]) {
				value = value + "\t" + my_p.getBest().fitness;
				time = time + "\t" + (System.currentTimeMillis() - start_time);
				it = it + "\t" + i;
				count++;
			}
			
			if (i%100000 == 0 || i == i_num - 1) {
				System.out.println(i + " value is " + my_p.getBest().fitness);
			}

			my_e.select(my_p);
			my_e.crossOver(my_p);
			my_e.mutation(my_p);
			fc.caculate(my_p.persons);
		}

		res = value + "\n" + time + "\n" + it + "\n";

		return res;
	}

	public static void write(String path, String content) {
		String s = new String();
		String s1 = new String();
		try {
			File f = new File(path);
			if (f.exists()) {
				System.out.println("文件存在");
			} else {
				System.out.println("文件不存在，正在创建...");
				if (f.createNewFile()) {
					System.out.println("文件创建成功！");
				} else {
					System.out.println("文件创建失败！");
				}
			}
			BufferedReader input = new BufferedReader(new FileReader(f));
			while ((s = input.readLine()) != null) {
				s1 += s + "\n";
			}

			input.close();
			s1 += content;
			BufferedWriter output = new BufferedWriter(new FileWriter(f));
			output.write(s1);
			output.close();
			System.out.println("文件内容:\n" + s1);


		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] arg) {
		System.out.println("enter main");
		double[] data = {10E6, 10E5}; 
		//double[] data = {24, 18, 4, 4, 2, 1}; 
		int dim = 63;
		//Fc4 fc = new FFZ41(dim);
		//Fc4 fc = new F43();
		Fc4 fc = new F42();
		//double[] data = { 0.9, 0.99, 0.999, 0.9999, 0.99999};
		//double[] data = {0.01, 0.1, 0.2, 0.4, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.999};
		//double[] data = {1.0E-12, 5.0E-11, 4.0E-11, 4.0E-11 , 2.0E-11};
		//double[] data = {1.0E12, 5.0E11, 4.0E11, 4.0E11 , 1.0E10, 1.0E09, 1.0E08, 1.0E07};
		//String tmp = ga(dim, 20, 0.1, 0.01, Long.MAX_VALUE, fc);
		
		int rp = 25;
		
		for (int i=0; i < rp; i++) {
			Calendar now=Calendar.getInstance(); 
			String time="_" + now.get(Calendar.YEAR)+(now.get(Calendar.MONTH)+1)+
			now.get(Calendar.DAY_OF_MONTH)+now.get(Calendar.HOUR_OF_DAY)+
			now.get(Calendar.MINUTE)+now.get(Calendar.SECOND);
			
			System.out.println("Begin repeat " + i + time);
			
			String tmp = ga(dim, Para4.pN*Para4.imn, Para4.p_cross, Para4.p_mut, Long.MAX_VALUE, fc);
			write("/home/stanley/Documents/paper_assistant/graduate/data/GA2", tmp);
		}


		System.out.println("leave main");
	}
}

